﻿using System;
using System.IO;
using MSExcel = Microsoft.Office.Interop.Excel;

namespace ExcelToHtml
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");

            var fileInfo = new FileInfo("Temp/生产完工数量.xlsx");
            Console.WriteLine(fileInfo.FullName);


            object filePathObject = fileInfo.FullName;
            var application = new MSExcel.ApplicationClass();

            var workbooks = application.Workbooks.Open(fileInfo.FullName);
            fileInfo = new FileInfo($"Temp/{Path.GetFileNameWithoutExtension(filePathObject.ToString())}.html");

            object savefilename = fileInfo.FullName;

            //进行另存为操作
            workbooks.SaveAs(savefilename, MSExcel.XlFileFormat.xlHtml, Type.Missing, Type.Missing, Type.Missing, Type.Missing, MSExcel.XlSaveAsAccessMode.xlNoChange, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
            object osave = false;
            //逐步关闭所有使用的对象
            workbooks.Close(osave, Type.Missing, Type.Missing);
            application.Quit();
            //垃圾回收
            System.Runtime.InteropServices.Marshal.ReleaseComObject(workbooks);
            System.Runtime.InteropServices.Marshal.ReleaseComObject(application.Application.Workbooks);
            System.Runtime.InteropServices.Marshal.ReleaseComObject(application);
            GC.Collect();
            //依据时间杀灭进程
            //System.Diagnostics.Process[] process = System.Diagnostics.Process.GetProcessesByName("EXCEL");
            //foreach (System.Diagnostics.Process p in process)
            //{
            //    if (DateTime.Now.Second - p.StartTime.Second > 0 && DateTime.Now.Second - p.StartTime.Second < 5)
            //    {
            //        p.Kill();
            //    }
            //}
        }
    }
}
